/*2S7LXljdWdGhrCxZ1MFl4i0/jcYg0CI/M/l35xRmsHHDM270ypZGxQFjXbKKRAXA*/
import React, { Component } from 'react';
import { base, ajax, toast,getMultiLang } from 'nc-lightapp-front';
const { NCButton: Button, NCTextArea: TextArea, NCModal: Modal,NCRow: Row, NCCol: Col, } = base;

import FormulaParam from './FormulaParam';
import './index.less';

/**
 * 
 * 计算器弹框
 * 
 * 接收父级参数：visible, pk_accountingbook, pk_org
 * 
 */

export default class FormulaModal extends Component {
	constructor(props) {
		super(props);
		this.state = {
			json:{},
			appcode: this.props.appcode,
			showFormulaParamModal: false, // 控制公式弹框是否显示
			paramType: '',   //选中的数据
			funcName: [],
			textValue: this.props.defaultValue,
			title: '',
			listItem: {},
		};
	}
	//打开弹框
	show = () => {
		this.props.modal.show('formula');
	}
	//关闭弹框
	close = () => {
		this.props.modal.close('formula');
	}
	// 清空
	handleClear = () => {
		this.setState({
			textValue: ''
		});
	}

	// 确定
	handleFormulaOK = () => {
		this.props.updatelistItem(this.state.listItem);
		let { textValue } = this.state;
		let { handleFormulaOK } = this.props;
		if (handleFormulaOK) {
			handleFormulaOK(textValue);
			this.handleClear();
		}
	}

	// 取消
	handleCancel = () => {
		let { handleCancel } = this.props;
		if (handleCancel) {
			handleCancel();
		}
	}

	// 展示公式弹框
	showFormulaParamModal = (item) => {
		if(item.funCode == 'SELFDEF_FromExcel'){
			let { textValue, json } = this.state;
			let defVal = json['publiccomponents-000156']; /* 国际化处理： SELFDEF_FromExcel(文件路径,页签,列,行)*/
			let tempStr = textValue ? textValue + defVal : '' + defVal;
			this.setState({
				textValue: tempStr
			});
		} else {
			this.setState({
				showFormulaParamModal: true,
				paramType: item
			});			
			if(item.funName.substring(0,3)=='跨账簿'){
				this.setState({
					showBook:true
				});
			}else{
				this.setState({
					showBook:false
				});
			}
		}
	}

	// 点击公式弹框确定按钮
	handleFormulaParamOK = (paramData) => {
		let paramDatas = paramData.formulaData;
		let { textValue } = this.state;
		let tempStr = textValue ? textValue + paramDatas : '' + paramDatas;
		let object = paramData.listItem;
		this.setState(
			{
				showFormulaParamModal: false,
				textValue: tempStr,
				listItem: object
			});
	}

	// 节点列表
	getFormulaName = () => {
		let url = '/nccloud/gl/glpub/getFuncs.do';
		let {nodeCode} = this.props;
		let sendData = {
			nodeCode: nodeCode //'transfer'
		};
		ajax({
			url,
			data: sendData,
			success: (response) => {
				const { data, success } = response;
				if (success) {
					//新增跨账簿函数
					data.push({funCode: 'BookOpenBal', funName: '跨账簿期初余额', display: '跨账簿期初余额(BookOpenBal)'});
					data.push({funCode: 'BookCloseBal', funName: '跨账簿期末余额', display: '跨账簿期末余额(BookCloseBal)'}       );
					data.push({funCode: 'BookAmt', funName: '跨账簿发生额', display: '跨账簿发生额(BookAmt)'}                );
					data.push({funCode: 'BookNetAmt', funName: '跨账簿净发生额', display: '跨账簿净发生额(BookNetAmt)'}        );
					data.push({funCode: 'BookAccumAmt', funName: '跨账簿累计发生额', display: '跨账簿累计发生额(BookAccumAmt)'}  );
					if (data) {
						this.setState({
							funcName: data
						});
					}
				} else {
					toast({ content: error.message, color: 'warning' });
				}
			},
			error: (res) => {
				toast({ content: res.message, color: 'warning' });
			}
		});
	}

	componentWillReceiveProps(nextProps) {
		let { textValue, title } = this.state;
		textValue = nextProps.defaultValue;
		title = nextProps.title;
		this.setState({ textValue, title });
		this.props.handleFormulaOK = nextProps.handleFormulaOK;
	}

	componentDidMount() {
		this.getFormulaName();
		this.props.FormulaModalRef(this)
	}

	// 列表渲染
	renderSpan = (funcName) => {
		return funcName.map((item, index) => {
			return <p className="nc-theme-common-font-c" fieldid={`${item.display}_item`} onClick={this.showFormulaParamModal.bind(this, item)}>{item.display}</p>;
		});
	}
	componentWillMount(){
		let callback= (json) =>{
            this.setState({
              	json:json
			})
        }
		getMultiLang({moduleId:'publiccomponents',domainName:'gl',currentLocale:'simpchn',callback});
    }
	modalContent = () => {
		let { textValue, funcName, json } = this.state;
		return(
			<div className="formulaform">
				<div className="des_title">
					<span className="nc-theme-common-font-c">{json['publiccomponents-000084']}：</span>{/* 国际化处理： 公式*/}
					<Button className="clear-btn" fieldid='clear' onClick={this.handleClear.bind(this)}>
						{json['publiccomponents-000085']}{/* 国际化处理： 清空*/}
					</Button>
				</div>
				<TextArea
					fieldid='formula_result'
					defaultValue={textValue}
					value={textValue}
					onChange={(v) => {
						this.setState({
							textValue: v
						});
					}}
				/>
				<Row>
					<Col md={24} xs={24} sm={24}>
						<div className="des_title nc-theme-common-font-c">{json['publiccomponents-000086']}：</div>{/* 国际化处理： 总账函数*/}
						{funcName ? <div className="func" fieldid='formula_list'>{this.renderSpan(funcName)}</div> : ''}
					</Col>
				</Row>
				
			</div>
		)
	}
	render() {
		let { pk_accountingbook, pk_org, modal } = this.props;
		let { json, title } = this.state;
		return (
			<div className="formulaModal">
				{modal.createModal('formula', {
					title: title || json['publiccomponents-000083'], /* 国际化处理： 金额公式*/
					size:'lg',
					content: this.modalContent(), //弹框内容，可以是字符串或dom
					beSureBtnClick: this.handleFormulaOK,//点击确定按钮事件
                	cancelBtnClick: this.handleCancel//取消按钮事件
				})}
				<FormulaParam
					appcode={this.state.appcode}
					pk_accountingbook={pk_accountingbook}
					pk_org={pk_org}
					paramType={this.state.paramType}
					visible={this.state.showFormulaParamModal}
					bookVisible={this.state.showBook}
					handleFormulaParamOK={this.handleFormulaParamOK.bind(this)}
					handleCancel={() => {
						this.setState({
							showFormulaParamModal: false
						});
					}}
				/>
			</div>
		);
	}
}

/*2S7LXljdWdGhrCxZ1MFl4i0/jcYg0CI/M/l35xRmsHHDM270ypZGxQFjXbKKRAXA*/